﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace CADDemo
{
    public static class AddEntityImpl
    {
        public static ObjectId AddEntity(this Database database, Entity entity)
        {
            ObjectId objectId = new ObjectId();
            using (Transaction tr = database.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(database.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                objectId = btr.AppendEntity(entity);
                tr.AddNewlyCreatedDBObject(entity, true);
                tr.Commit();
            }
            return objectId;
        }
    }

    public class AddEntity
    {
        [CommandMethod("AddLine")]
        public void AddLine()
        {
            Database database = HostApplicationServices.WorkingDatabase;

            Point3d startPoint = new Point3d(0, 0, 100);
            Point3d endPoint = new Point3d(100, 100, 0);

            Line line = new Line(startPoint, endPoint);

            ObjectId entityId =  database.AddEntity(line);
        }
    }


}
